package com.beiding.jsonanalyzer.analyze.query.queryfunc;

import com.beiding.jsonanalyzer.analyze.Undefind;
import com.beiding.jsonanalyzer.analyze.query.Comparator;
import com.beiding.jsonanalyzer.analyze.query.ComparatorResult;

import java.util.Date;

/**
 * @author 丁常磊
 * @date 2022/6/27 10:55
 */

public class Eq implements Comparator {

    @Override
    public ComparatorResult compare(Object keyValue, Object valueValue) {

        //如果有一方未被定义,返回无结果
        if (keyValue == Undefind.i || valueValue == Undefind.i) {
            return null;
        }

        if (keyValue == null) {
            return valueValue == null ? ComparatorResult.TRUE : ComparatorResult.FALSE;
        } else {
            if (keyValue instanceof Number) {
                if (valueValue instanceof Number) {
                    return ((Number) keyValue).doubleValue() == ((Number) valueValue).doubleValue() ? ComparatorResult.TRUE : ComparatorResult.FALSE;
                } else {
                    return ComparatorResult.FALSE;
                }
            } else if (keyValue instanceof Date) {
                if (valueValue instanceof Date) {
                    return ((Date) keyValue).getTime() == ((Date) valueValue).getTime() ? ComparatorResult.TRUE : ComparatorResult.FALSE;
                } else {
                    return ComparatorResult.FALSE;
                }
            } else {
                return keyValue.equals(valueValue) ? ComparatorResult.TRUE : ComparatorResult.FALSE;
            }
        }
    }

    @Override
    public String toString() {
        return "=";
    }


}